/*
 * This file is part of jVisualFS.
 * Version: release-0.8_beta
 *
 * jVisualFS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * jVisualFS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with jVisualFS.  If not, see <http://www.gnu.org/licenses/>.
 */

package jvisualfs;

import java.util.Vector;

/**
 *
 * @author Dag Rune Sneeggen
 */
public class DirCollection {
    // Fields declarations
    private Vector dirs;
    
    
    // Constructor
    public DirCollection() {
        this.dirs = new Vector();
    }
    
    
    // Methods
    public DirStruct add(DirStruct dir) {
        dirs.add(dir);
        //return (DirStruct) dirs.get(dirs.size());
        return dir;
    }
    public void clear() {
        dirs.clear();
    }
    public void sort() {        
        Vector SortedDirs = new Vector(this.dirs.size());
        SortedDirs.add(0, dirs.get(0));
        for (int l = 1; l < dirs.size(); l++) {
            boolean isInserted = false;
            DirStruct lDir = (DirStruct) dirs.get(l);
            for (int m = 0; m < SortedDirs.size(); m++) {
                DirStruct mDir = (DirStruct) SortedDirs.get(m);
                if (lDir.getLength() > mDir.getLength()) {
                    SortedDirs.insertElementAt(lDir, m);
                    isInserted = true;
                    break;
                }
            }
            if (!isInserted) {
                SortedDirs.add(lDir);
            }
        }
        this.dirs = SortedDirs;
    }
    public DirStruct get(String name) {
        int i = 0;
        while (i < this.dirs.size()) {
            //System.out.println("SISSS " + this.dirs.size() + "IIIiii " + i);
            DirStruct tmp = (DirStruct) this.dirs.get(i);
            //System.out.println(tmp.getName()+ " 0.0.0.0 " + name);
            if (tmp.getName().equals(name)) {
                return (DirStruct) tmp;
            }
            i++;
        }
         return null;
    }
    
    public int size() {
        return dirs.size();
    }
    public DirStruct getNum(int k) {
        DirStruct tmp = (DirStruct) this.dirs.get(k);
        return (DirStruct) tmp;            
    }
}
